package com.rongzhe.house.ui.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.rongzhe.house.R;
import com.rongzhe.house.manager.UserManager;
import com.rongzhe.house.presenter.MainPresenter;
import com.rongzhe.house.ui.fragment.FragmentMain;
import com.rongzhe.house.ui.fragment.HouseEntrustmentFragment;
import com.rongzhe.house.ui.fragment.HouseEntrustmentFragment2;
import com.rongzhe.house.ui.fragment.MyFavoriteFragment;
import com.rongzhe.house.ui.fragment.UserCenterFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.rongzhe.house.AppConfig.IMAGE_URL_BASE;

/**
 * Created by zhongyanli on 2017/11/9.
 */

public class MainActivity extends BaseActivity<MainPresenter> {

    @BindView(R.id.radio_home)
    RadioButton radioHome;
    @BindView(R.id.raido_house_entrust)
    RadioButton raidoHouseEntrust;
    @BindView(R.id.raido_favorite)
    RadioButton raidoFavorite;
    @BindView(R.id.raido_mine)
    RadioButton raidoMine;
    @BindView(R.id.raido_group_navigation)
    RadioGroup raidoGroupNavigation;
    @BindView(R.id.container_main)
    FrameLayout containerMain;

    private HouseEntrustmentFragment2 mHouseEntrustmentFragment;
    private MyFavoriteFragment mMyFavoriteFragment;
    private UserCenterFragment mUserCenterFragment;
    private FragmentMain mMainFragment;
    private Fragment mCurrentFragment = null;

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        radioHome.setChecked(true);
        initFramgent();
    }

    private void initFramgent() {
        mHouseEntrustmentFragment = new HouseEntrustmentFragment2();
        mMyFavoriteFragment = new MyFavoriteFragment();
        mUserCenterFragment = new UserCenterFragment();
        mMainFragment = new FragmentMain();
        int mainContainerId = R.id.container_main;
        getSupportFragmentManager().beginTransaction()
                .add(mainContainerId, mHouseEntrustmentFragment)
                .add(mainContainerId, mMyFavoriteFragment)
                .add(mainContainerId, mUserCenterFragment)
                .add(mainContainerId, mMainFragment).commitNow();
        showFragment(mMainFragment);
        raidoGroupNavigation.setOnCheckedChangeListener(mNavListener);
    }

    @SuppressLint("RestrictedApi")
    private void showFragment(Fragment showingFragment) {
        if (showingFragment == null) {
            showingFragment = mMainFragment;
        }
        if (mCurrentFragment != null && mCurrentFragment.equals(showingFragment)) {
            return;
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            if (!fragment.equals(showingFragment)) {
                fragmentTransaction.hide(fragment);
            }
        }
        fragmentTransaction.show(showingFragment);
        fragmentTransaction.commit();
        mCurrentFragment = showingFragment;
        //更新actionbar
        configActionBar();

        if (showingFragment != null) {
            setCurrentChecked();
        }
    }

    private void setCurrentChecked() {
        int checkedId = raidoGroupNavigation.getCheckedRadioButtonId();
        if (mCurrentFragment.equals(mMainFragment)) {
            if (checkedId != R.id.radio_home) {
                radioHome.setChecked(true);
            }
        } else if (mCurrentFragment.equals(mHouseEntrustmentFragment)) {
            if (checkedId != R.id.raido_house_entrust) {
                raidoHouseEntrust.setChecked(true);
            }
        } else if (mCurrentFragment.equals(mMyFavoriteFragment)) {
            if (checkedId != R.id.raido_favorite) {
                raidoFavorite.setChecked(true);
            }
        } else if (mCurrentFragment.equals(mUserCenterFragment)) {
            if (checkedId != R.id.raido_mine) {
                raidoMine.setChecked(true);
            }
        }
    }

    private RadioGroup.OnCheckedChangeListener mNavListener
            = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.radio_home: {
                    showFragment(mMainFragment);
                    break;
                }
                case R.id.raido_house_entrust: {
                    showFragment(mHouseEntrustmentFragment);
                    break;
                }
                case R.id.raido_favorite: {
                    showFragmentWithLoginChecking(mMyFavoriteFragment);
                    break;
                }
                case R.id.raido_mine: {
                    showFragmentWithLoginChecking(mUserCenterFragment);
                    break;
                }
            }
        }
    };

    private void showFragmentWithLoginChecking(Fragment fragment) {
        if (UserManager.getInstance().isLogin()) {
            showFragment(fragment);
        } else {
            //将radio button选中状态还回去
            setCurrentChecked();
            launchActivity(true, null);
        }
    }

    @Override
    protected void configActionBar() {
        if (mCurrentFragment != null && mCurrentFragment.equals(mMainFragment)) {
            getSupportActionBar().setCustomView(R.layout.action_bar_main);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            getSupportActionBar().setDisplayShowCustomEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().getCustomView().findViewById(R.id.btn_current_city)
                    .setOnClickListener(mCityListener);
            getSupportActionBar().getCustomView().findViewById(R.id.btn_search)
                    .setOnClickListener(mSearchListener);
            //去除actionbar兩邊的padding
            Toolbar parent = (Toolbar) getSupportActionBar().getCustomView().getParent();
            parent.setContentInsetsAbsolute(0, 0);
            getSupportActionBar().show();
        } else {
            super.configActionBar();
        }
    }

    private View.OnClickListener mSearchListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPresenter.onSearClick();
        }
    };

    private View.OnClickListener mCityListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                mPresenter.onCurrentCityClick();
        }
    };

    @Override
    public void onBackPressed() {
        if (mCurrentFragment != null && mCurrentFragment.equals(mHouseEntrustmentFragment)) {
            if (mHouseEntrustmentFragment.handleBackPressed()) {
                return;
            }
        }
        if(mCurrentFragment == null || !mCurrentFragment.equals(mMainFragment)) {
            showFragment(mMainFragment);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected boolean showActionBar() {
        if (mCurrentFragment != null && mCurrentFragment.equals(mUserCenterFragment)) {
            return false;
        }
        return true;
    }

    @Override
    protected String getActionTitle() {
        if (mCurrentFragment == null) {
            return null;
        }
        if (mCurrentFragment.equals(mHouseEntrustmentFragment)) {
            return  getString(R.string.house_entrust);
        } else if (mCurrentFragment.equals(mMyFavoriteFragment)) {
            return getString(R.string.favorite);
        }
        return null;
    }

    public void setImgHead(Bitmap finalPhotoBmp) {
        mUserCenterFragment.setImgHead(finalPhotoBmp);
    }

    public void setImgHead(String url) {
        mUserCenterFragment.setImgHead(url);
    }
}
